<?php
class Ddup_Tags_Block_Product_Result extends Mage_Tag_Block_Product_Result
{
    protected function _prepareLayout()
    {
        $title = $this->getHeaderText();
        $description = $this->getHeaderDescription();
        $keywords = $this->getHeaderKeywords();
        $this->getLayout()->getBlock('head')->setTitle($title);
        $this->getLayout()->getBlock('root')->setHeaderTitle($title);
        $this->getLayout()->getBlock('head')->setDescription($description);
        $this->getLayout()->getBlock('head')->setKeywords($keywords);
        return parent::_prepareLayout();
    }

    public function getHeaderText()
    {
        if( $this->getTag()->getName() ) {
        	$titleTemplate = Mage::getStoreConfig('tags/settings/title_template');
        	$titleTemplate = str_replace('{tag}', '%s', $titleTemplate);
            return Mage::helper('tag')->__($titleTemplate, $this->htmlEscape($this->getTag()->getName()));
        } else {
            return false;
        }
    }
    
    public function getHeaderDescription()
    {
    	$tagName = $this->getTag()->getName() ? $this->getTag()->getName() : '';
    	$descriptionTemplate = Mage::getStoreConfig('tags/settings/meta_description_template');
    	$descriptionTemplate = str_replace('{tag}', '%s', $descriptionTemplate);
    	return Mage::helper('tag')->__($descriptionTemplate, $this->htmlEscape($this->getTag()->getName()));
    }
    
    public function getHeaderKeywords()
    {    	
    	$tagName = $this->getTag()->getName() ? $this->getTag()->getName() : '';
    	$keywordsTemplate = Mage::getStoreConfig('tags/settings/meta_keywords_template');
    	$keywordsTemplate = str_replace('{tag}', '%s', $keywordsTemplate);
    	return Mage::helper('tag')->__($keywordsTemplate, $this->htmlEscape($this->getTag()->getName()));
    }
}
